<template>
    <div class="xunjingwendao">
        <Navbar :activeMenu="currentPath" />
        <div class="main container">
            <!-- 收货地址 -->
            <div>
                <span style="font-weight: 800;font-size: 16px;padding-left: 10px;">确认收货地址</span>
            </div>

            <div class="addr">

                <div class="order-right" v-if="recinfo.length > 0">
                    <div :class="['tel-name', tab == index ? 'active' : '']" v-for="(recinfo, index) in recinfo"
                        :key="index" @click="selectDz(index, recinfo)">
                        <div>
                            <img v-if="tab == index" src="@/assets/image/dz1.png" alt="">
                            <img v-else src="@/assets/image/dz.png" alt="">
                        </div>
                        <div class="dz">
                            <div style="padding-right: 20px;    font-size: 12px;"> {{ recinfo.provinceName }} {{
                                recinfo.cityName }} {{ recinfo.countyName }} </div>
                            <div
                                style="font-size: 14px; font-weight: 600;line-height: 14px;margin: 0 8px 8px 0;color: #000;">
                                {{
                                    recinfo.detailInfo }}</div>
                            <div class="name" style="font-size: 12px;">
                                <span> {{ recinfo.userName }}</span><span> {{ recinfo.telNum }}</span>
                            </div>
                        </div>


                    </div>


                </div>


                <div class="" v-else @click="add"
                    style="text-align: center;display: flex;justify-content: center;width: 100%;">
                    <el-empty description="去添加收货地址"></el-empty>
                </div>

            </div>
            <!-- 购买商品列表 -->
            <div class="buy-list">
                <div class="row" v-for="(row, index) in buylist" :key="index">
                    <div class="goods-info">
                        <div class="img">
                            <img :src="row.picUrls" />
                        </div>
                        <div class="info">
                            <div class="order-title">{{ row.name }}</div>
                            <div class="order-spec">数量:{{ row.number }}</div>

                            <div class="price-number">
                                <div class="price">￥{{ row.salesPrice * row.number }}</div>

                                <div class="number">

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 提示-备注 -->
            <div class="order">

                <div class="row">
                    <div class="nominal" style="font-size: 16px;">
                        备注：
                    </div>
                    <div class="right">
                        <el-input placeholder="选填,备注内容" v-model="note"></el-input>
                    </div>
                </div>
            </div>
            <!-- 明细 -->
            <div class="detail">
                <div class="row">
                    <div class="nominal">
                        商品金额
                    </div>
                    <div class="content">
                        ￥{{ goodsPrice | toFixed }}
                    </div>
                </div>
                <div class="row">
                    <div class="nominal">
                        运费
                    </div>
                    <div class="content">
                        ￥+{{ freight | toFixed }}
                    </div>
                </div>

            </div>

            <div class="order_footer">
                <div class="settlement">
                    <div class="sum">合计:<div class="money">￥{{ goodsPrice }}</div>
                    </div>
                    <div class="btn" @click="toPay">提交订单</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { useraddress } from '@/api/index'
export default {
    computed: {
        currentPath() {
            return '/shop'; // 获取当前路由路径
        },
    },
    data() {
        return {
            tab: 0,
            buylist: [],		//订单列表
            goodsPrice: 0.0,	//商品合计价格
            sumPrice: 0.0,	//用户付款价格
            freight: 0.00,	//运费
            note: '',		//备注
            int: 1200,		//抵扣积分
            deduction: 0,	//抵扣价格
            recinfo: [],
            address: {}

        };
    },

    mounted(e) {
        this.addressList()
        let arr = []

        if (this.$route.query.type == 'order') {
            let data = JSON.parse(this.$route.query.goodsData)
            console.log(data)
            this.goodsPrice = parseFloat(data.salesPrice * data.number).toFixed(2)
            arr.push(data)
            this.buylist = arr;
        } else if (this.$route.query.type == 'cart') {
            let data = JSON.parse(this.$route.query.goodsData)
            let newData = []
            data.forEach((v) => {
                newData.push({
                    name: v.spuName,
                    number: v.quantity,
                    picUrls: v.picUrl,
                    salesPrice: v.addPrice,
                    spuId: v.spuId,
                })
            })

            this.buylist = newData;
            this.goodsPrice = this.$route.query.sumPrice;
        }




    },

    filters: {
        toFixed: function (x) {
            return parseFloat(x).toFixed(2);
        }
    },
    methods: {
        add() {
            console.log(11)
            uni.navigateTo({
                url: "/pages/address/edit?type=add"
            })
        },
        addressList() {
            useraddress({ pageNum: 1, pageSize: 100 }).then((res) => {
                console.log(res)
                if (res.code != 200) {
                    this.$message.error(res.msg)
                    return
                }
                let data = res.data.records;
                this.recinfo = data;
                if (data.length == 0) {
                    return
                }
                let index = data.findIndex((v) => {
                    return v.isDefault == 1
                })
                if (index != -1) {
                    this.address = data[index]
                    this.tab = index
                }

            })
        },
        clearOrder() {
            uni.removeStorage({
                key: 'buylist',
                success: (res) => {
                    this.buylist = [];
                    console.log('remove buylist success');
                }
            });
        },
        toPay() {
            if (this.address == null) {
                this.$message.error('请选择收货地址')
                return
            }

            this.$router.push({
                path: '/payShopMoney',
                query: {
                    orderData: JSON.stringify(this.buylist),
                    userMessage: this.note,
                    userAddressId: this.address.id,
                    goodsPrice: this.goodsPrice,
                    type: 'normal'
                }
            })
        },
        //选择收货地址
        selectAddress() {
            uni.navigateTo({
                url: '/pages/address/edit?type=select'
            })
        },
        selectDz(index, row) {
            this.tab = index;
            this.address = row;

        }
    }
}
</script>

<style lang="scss">
.addr {
    width: 100%;
    padding: 10px 10px;
    border: 1px solid #f5f5f5;
    border-radius: 20px;
    display: flex;

    .order-right {
        display: flex;
        margin-left: 10px;
        flex-wrap: wrap;

        .active {
            border: 1px solid #f06c7a;
        }
    }

    .icon {
        width: 80px;
        height: 80px;


    }

    .tel-name {
        width: 100%;
        border: 1px solid #f5f5f5;
        display: flex;
        padding: 15px;
        width: 18%;
        margin: 10px;


        img {
            width: 20px;
        }

        .dz {
            margin-left: 13px;
        }

        .tel {
            margin-left: 20px;
        }
    }

    .addres {
        width: 100%;
        font-size: 14px;
        color: #999;
    }
}

.buy-list {
    width: 86%;
    padding: 10px 3%;
    margin: 30px auto 20px auto;
    border: 1px solid #f5f5f5;
    border-radius: 20px;

    .row {
        margin: 30px 0;

        .goods-info {
            width: 100%;
            display: flex;

            .img {
                width: 210px;
                height: 120px;
                border-radius: 10px;
                overflow: hidden;
                flex-shrink: 0;
                margin-right: 10px;

                img {
                    width: 100%;
                    height: 100%;

                }


            }

            .info {
                width: 100%;


                .order-title {
                    width: 100%;
                    font-size: 16px;
                    display: -webkit-box;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 2;
                    // text-align: justify;
                    overflow: hidden;
                }

                .order-spec {
                    font-size: 14px;

                    color: #a7a7a7;
                    height: 40px;
                    display: flex;
                    align-items: center;
                    padding: 0 10px;
                    border-radius: 20px;

                }

                .price-number {
                    width: 100%;
                    display: flex;
                    justify-content: space-between;
                    align-items: flex-end;
                    font-size: 16px;
                    height: 40px;

                    .price {
                        color: #f06c7a;
                    }

                    .number {
                        display: flex;
                        justify-content: center;
                        align-items: center;

                    }
                }
            }
        }
    }
}

.order {
    width: 86%;
    padding: 10px 3%;
    margin: 30px auto 20px auto;
    border: 1px solid #f5f5f5;
    border-radius: 20px;

    .row {
        margin: 20px 0;
        height: 40px;
        display: flex;
        align-items: center;

        .order_left {
            font-size: 16px;
        }

        .order_right {
            margin-left: 40px;
            font-size: 16px;
            color: #999;

            input {
                font-size: 26px;
                color: #999;
            }
        }
    }
}

.blck {
    width: 100%;
    height: 100px;
}

.order_footer {
    width: 92%;


    height: 100px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    font-size: 16px;


    z-index: 5;

    .settlement {
        width: 80%;
        display: flex;
        justify-content: flex-end;
        align-items: center;

        .sum {
            width: 50%;
            font-size: 16px;
            margin-right: 10px;
            display: flex;
            justify-content: flex-end;
            color: #91512b;

            .money {
                font-weight: 600;
            }
        }

        .btn {

            height: 40px;
            width: 90px;
            background-color: #91512b;
            color: #fff;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 14px;
            border-radius: 10px;
        }
    }
}

.detail {
    width: 86%;
    padding: 10px 3%;
    margin: 30px auto 20px auto;
    border: 1px solid #f5f5f5;
    border-radius: 20px;

    .row {
        height: 60px;
        display: flex;

        align-items: center;

        .nominal {
            font-size: 16px;
        }

        .content {
            font-size: 16px;
            color: #f06c7a;
            margin-left: 15px;
        }
    }
}
</style>
